package com.kim.smartring.utils;

import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import com.kim.smartring.ble.BleNamesResolver;
import com.kim.smartring.ble.BleWrapper;

import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.util.Log;

public class BLEDataUtils {

	private static final String TAG = "BLEDataBuilder";

	private static final HashMap<String, String> weekdays = new HashMap<String, String>();
	static {
		weekdays.put("1", "01");
		weekdays.put("2", "02");
		weekdays.put("3", "04");
		weekdays.put("4", "08");
		weekdays.put("5", "10");
		weekdays.put("6", "20");
		weekdays.put("7", "40");
	}

	public static String buildAlarmData(int hour, int minute, int weekday) {
		StringBuilder builder = new StringBuilder("0x03");
		builder.append(weekdays.get(String.valueOf(weekday)))
				.append(Integer.toHexString(hour))
				.append(Integer.toHexString(minute));
		Log.d(TAG, String.format("buildAlarmData:%s", builder.toString()));
		return builder.toString();
	}

	public static String buildLossLinkAlert() {
		StringBuilder builder = new StringBuilder("0x02");
		Log.d(TAG, String.format("buildLossLinkAlert:%s", builder.toString()));
		return builder.toString();
	}

	public static void writeDataToSmartRing(BleWrapper bleWrapper, String data,
			String serviceUUID, String characteristicUUID) {
		List<BluetoothGattService> bluetoothGattServices = bleWrapper
				.getCachedServices();
		if (bluetoothGattServices == null || bluetoothGattServices.size() == 0) {
			return;
		}
		for (BluetoothGattService bluetoothGattService : bluetoothGattServices) {
			String uuid = bluetoothGattService.getUuid().toString();
			if (serviceUUID.equals(uuid)) {
				Log.d(TAG, String.format("sendMsgDetailsToRing->serviceName:",
						BleNamesResolver.resolveServiceName(uuid)));
				BluetoothGattCharacteristic characteristic = bluetoothGattService
						.getCharacteristic(UUID.fromString(characteristicUUID));
				bleWrapper.writeDataToCharacteristic(characteristic,
						HexStringUtils.parseHexStringToBytes(data));
				return;
			}
		}
	}

	public static void enableNofity(BleWrapper bleWrapper, String serviceUUID,
			String characteristicUUID, boolean enable) {
		List<BluetoothGattService> bluetoothGattServices = bleWrapper
				.getCachedServices();
		if (bluetoothGattServices == null || bluetoothGattServices.size() == 0) {
			return;
		}
		for (BluetoothGattService bluetoothGattService : bluetoothGattServices) {
			String uuid = bluetoothGattService.getUuid().toString();
			if (serviceUUID.equals(uuid)) {
				Log.d(TAG, String.format("sendMsgDetailsToRing->serviceName:",
						BleNamesResolver.resolveServiceName(uuid)));
				BluetoothGattCharacteristic characteristic = bluetoothGattService
						.getCharacteristic(UUID.fromString(characteristicUUID));
				bleWrapper.setNotificationForCharacteristic(characteristic,
						enable);
				return;
			}
		}
	}
}
